﻿<Table TItem="CustomDynamicData" OnQueryAsync="OnQueryAsync"
        IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
    <TableColumns>
        <TableColumn @bind-Field="@context.Fix" Sortable="true" Filterable="true" />
        @foreach (var element in DynamicColumnList)
        {
            <TableColumn Field="@element.ToString()" FieldName="@element.ToString()" Text="@element" Sortable="true" Filterable="true">
                <Template Context="v">
                    <div>Template - @v.Value</div>
                </Template>
            </TableColumn>
        }
    </TableColumns>
</Table>

@code {
    private static List<string> StaticColumnList => new() { "A", "B", "C", "Z" };

    [NotNull]
    private List<string>? DynamicColumnList { get; set; }

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        // 构造动态列
        var now = DateTime.Now;
        DynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
    }

    private readonly static Random random = new();

    private Task<QueryData<CustomDynamicData>> OnQueryAsync(QueryPageOptions options)
    {
        var items = Enumerable.Range(1, 10)
            .Select(index => new CustomDynamicData(index.ToString(), DynamicColumnList.ToDictionary(d => d.ToString(), d => $"{random.Next(1000, 9999)}")));

        // sort logic ...
        // filter logic ...

        return Task.FromResult(new QueryData<CustomDynamicData>()
        {
            Items = items,
            TotalCount = 10,
            IsSorted = true,
            IsFiltered = true
        });
    }
}
